WEBVTT

00:01.500 --> 00:06.330
Hello and welcome back to part two of our user defined function session.

00:06.330 --> 00:13.270
So far we only have one parameter in our functions but of course functions can also have many parameters.

00:13.350 --> 00:18.500
So let's define the function power very calculate x to the power of y

00:22.450 --> 00:24.660
so you know the function definition within the brackets.

00:24.670 --> 00:32.070
We have the parameters X and Y and we return x to the power of y and then we can call the function by

00:32.080 --> 00:35.210
passing the arguments 2 and 7.

00:35.230 --> 00:46.160
And we would expect the two to the power of seven and it's 128 and before we move on I want to make

00:46.160 --> 00:49.310
clear the difference between perimeter and argument.

00:49.310 --> 00:54.000
So many people mix those two up and also I do this sometimes.

00:54.110 --> 00:57.620
So here in this function x and y are paramount.

00:57.640 --> 01:06.420
So these are the names that appear in the function definition so X and Y are our parameters and year

01:06.430 --> 01:13.350
2 and 7 arguments all values that are passed to the functions when the function is called.

01:13.360 --> 01:19.420
So this is a slight difference and I just want to to make it clear and if you mix it up it's actually

01:19.420 --> 01:20.310
not so serious.

01:20.320 --> 01:25.320
So everybody understands what you mean by saying argument or parameter.

01:25.780 --> 01:27.080
And in this example here.

01:27.090 --> 01:37.190
So 2 and 7 are two positional arguments because Python automatically assumes that two stands for X and

01:37.230 --> 01:45.570
seven for Y because exists the first parameter and y is the second parameter and consequently two is

01:45.770 --> 01:53.970
the argument for the first parameter and 7 the argument for the second parameter and you can also make

01:53.970 --> 01:55.380
this more explicit.

01:55.410 --> 01:59.950
And by saying okay exist too and Y is seven.

02:00.720 --> 02:08.060
And in this case 2 1 7 are so-called keyword arguments because you explicitly say okay.

02:08.080 --> 02:11.270
And my part of me to exist 2 and my parameter y is 7

02:14.450 --> 02:20.560
so in this case of course the same side and what you can do you can change the position of the keyword

02:20.600 --> 02:21.170
arguments.

02:21.170 --> 02:27.830
So instead of saying X is true into y 7 you can also say y 7 and X is 2 that's the problem.

02:27.830 --> 02:34.860
So there's no danger that the Python could mix it up.

02:34.890 --> 02:42.150
Now what if we use a keyword argument for the parameter X and a persistent argument for parameter y.

02:43.110 --> 02:44.810
So let's look what we get here.

02:45.990 --> 02:54.520
And here we get the error message position argument follows keyword argument so whenever you use a mix

02:54.520 --> 02:59.970
of a keyword and positional arguments the positional arguments have to come first.

02:59.980 --> 03:05.090
So you must not start with the keyboard arguments otherwise to get an error message.

03:07.950 --> 03:15.250
But what you can do you can pass them the positional argument to for the parameter X and then apply

03:15.250 --> 03:19.440
the keyword argument 7 to about parameter y.

03:19.470 --> 03:28.330
So this works and what we can also do we can define default values for all parameters so we can define

03:28.360 --> 03:36.010
the function power to with the default values to for our paramedics and seven for our parameter y

03:40.620 --> 03:45.480
and then we can call our function without any arguments over so with the default values

03:49.450 --> 03:55.730
and of course we can also call our function with the new values and to positional arguments.

03:55.810 --> 04:00.410
So three for our perimeter except to follow our perimeter by

04:05.850 --> 04:12.100
and instead of using two positional arguments 3 and 2 we can also use to keyword arguments.

04:12.730 --> 04:15.490
X equals three and Y equals two.

04:16.450 --> 04:20.760
So this is more explicit and they are actually better to read for everyone

04:26.390 --> 04:34.040
now what they fear only wanted change parameter X so we have by default x equals two and Y equals seven.

04:34.040 --> 04:43.200
And if we say that okay except B three then Python automatically assumes the default value for y.

04:43.420 --> 04:52.350
So this works in three to the power of seven and if you only want to change X you can also take a new

04:52.350 --> 05:01.360
position the argument for our parameter X so if you only pass here the argument three paces automatically

05:01.600 --> 05:11.830
assumes that three it stands for X and we have the default value 7 for y and this gives them the same

05:11.830 --> 05:12.330
result.

05:15.320 --> 05:18.230
Now what if we only want to change our parameter y.

05:19.100 --> 05:26.720
So if we use a positional argument for why and place it here at the beginning and Python has a problem

05:26.750 --> 05:29.660
the cost Python thinks okay.

05:30.090 --> 05:37.340
3 stands for X and then if you define x equals 2 then our function got multiple values for the argument

05:37.400 --> 05:37.820
X

05:40.980 --> 05:42.120
and same as before.

05:42.120 --> 05:48.780
So if we start with them the keyword argument for x x equals 2 and then assume that Y is 3.

05:48.780 --> 05:50.330
This also does not work.

05:52.840 --> 05:54.630
Because position arguments.

05:54.650 --> 05:55.380
Uh here.

05:55.410 --> 05:55.670
Um.

05:55.690 --> 05:59.470
3 for y follows and the keyboard argument and this does not work.

06:01.330 --> 06:09.180
But what we can do we can just say okay our Y is 3 and 4 x we want to have our default value networks

06:12.220 --> 06:19.270
and we can make it more explicit by passing the 2 keyboard arguments or saying Okay X is 2 It's the

06:19.270 --> 06:27.240
same s and the default value and why should be 3 and in my opinion this is definitely best practice

06:27.480 --> 06:31.020
because everybody sees and knows at a first glance.

06:31.190 --> 06:31.410
Yeah.

06:31.410 --> 06:35.150
The arguments for for the parameters exist to win.

06:35.150 --> 06:35.960
Y is 3

06:39.870 --> 06:40.460
all right.

06:40.470 --> 06:47.790
So now coming to a completely different topic we can also assign a variety to our return value.

06:47.900 --> 06:56.030
So yeah our function power to returns um the value 8 in this case and we can directly assign the arrival

06:56.120 --> 07:01.690
set so set should be 8 and this correct.

07:01.700 --> 07:02.050
All right.

07:02.060 --> 07:08.150
So this last part 2 of user defined functions and in the next session we will learn that a function

07:08.150 --> 07:14.600
can also return multiple values and we will also have a look at uh yeah two case studies so hope to

07:14.600 --> 07:15.530
see you there by.
